package com.example.administrator.xiaoxiang.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.entity.ShopingcartEntity;
import com.example.administrator.xiaoxiang.gen.DaoMaster;
import com.example.administrator.xiaoxiang.gen.ShopingcartEntityDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页中的搜索界面
 */
public class SearchActivity extends AppCompatActivity {

    private ShopingcartEntityDao shopingcartEntityDao;
    private List<String> data;
    private ListView listView;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        init();


    }

    private void init() {
        listView = (ListView) findViewById(R.id.lv);
        data = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "xiaoxiang.db", null);
        SQLiteDatabase db = openHelper.getReadableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        shopingcartEntityDao = daoMaster.newSession().getShopingcartEntityDao();


    }

    public void click(View view) {
        List<ShopingcartEntity> list =
                shopingcartEntityDao.queryBuilder().build().list();
        for (ShopingcartEntity shopingcartEntity : list) {
            data.add(shopingcartEntity.toString());
        }
        adapter.notifyDataSetChanged();
    }
}
